package cn.edu.ctbu.finaltest.controller;

import cn.edu.ctbu.finaltest.core.PageUtils;
import cn.edu.ctbu.finaltest.domain.SelectCourse;
import cn.edu.ctbu.finaltest.domain.Student;
import cn.edu.ctbu.finaltest.exception.REnum;
import cn.edu.ctbu.finaltest.exception.RException;
import cn.edu.ctbu.finaltest.service.CourseService.CourseService;
import cn.edu.ctbu.finaltest.service.SelectCourseService.SelectCourseService;
import cn.edu.ctbu.finaltest.service.StudentCourseMessageService.StudentCourseMessageService;
import cn.edu.ctbu.finaltest.service.StudentSelectCourseService.StudentSelectCourseService;
import cn.edu.ctbu.finaltest.service.StudentService.StudentService;
import cn.edu.ctbu.finaltest.service.TeacherCourseService.TeacherCourseService;
import cn.edu.ctbu.finaltest.service.TeacherService.TeacherService;
import cn.edu.ctbu.finaltest.vo.StudentCourseMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SelectCourseService selectCourseService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherCourseService teacherCourseService;
    @Autowired
    private StudentCourseMessageService studentCourseMessageService;
    @Autowired
    private StudentSelectCourseService studentSelectCourseService;

    /**
     * 页面跳转
     * @return
     */
    @RequestMapping("/student/studentList.html")
    public String studentListDisplay(){return "student/studentList";}
    @RequestMapping("/student/studentSelectCourse.html")
    public String studentSelectCourseDisplay(){return "student/studentSelectCourse";}
    @RequestMapping("/student/loginScreen.html")
    public String administratorLoginScreen(){return "login/loginScreen";}


    /**
     * 用于显示页面左上角的名字
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/student/showName")
    public String studentName(HttpServletRequest httpServletRequest) throws Exception{
        HttpSession session = httpServletRequest.getSession();
        if (!session.getAttribute("choose").equals("1") || session.getAttribute("userid") == null){
            throw new RException(REnum.LOGIN_ERR);
        }
        Student studentMessage = (Student) session.getAttribute("user");
        return studentMessage.getSname();
    }

    /**
     * 完成成绩查询里的查询功能
     * @param size
     * @param pageNum
     * @param name
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/student/search")
    public PageUtils studentSearch(@RequestParam(value = "size",defaultValue = "10") Integer size,
                                   @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                   @RequestParam(value = "name",defaultValue = "") String name,
                                   HttpServletRequest httpServletRequest) throws Exception {
        HttpSession session = httpServletRequest.getSession();
        if (!session.getAttribute("choose").equals("1") || session.getAttribute("userid") == null){
            throw new RException(REnum.LOGIN_ERR);
        }
        String[] arrayList = name.split("!");

        name = arrayList[0];
        String searchOption = arrayList[1];
        String searchMatching = arrayList[2];

        List<StudentCourseMessage> studentCourseMessages = new ArrayList<>();

        PageUtils allStudentList = studentGetByPage(0, 2000000000, 1, httpServletRequest);

        if (searchMatching.equals("20")) {//模糊查询
            if (searchOption.equals("10")) {//课号查询
                studentCourseMessages = studentCourseMessageService.findByCnoLike(name, (List<StudentCourseMessage>) allStudentList.getRows());
            } else if (searchOption.equals("11")) {//以课程名查询
                studentCourseMessages = studentCourseMessageService.findByCnameLike(name, (List<StudentCourseMessage>) allStudentList.getRows());
            } else {//以教师名查询
                studentCourseMessages = studentCourseMessageService.findByTeacherLike(name, (List<StudentCourseMessage>) allStudentList.getRows());
            }
        } else {//精确查询
            if (searchOption.equals("10")) {//课号查询
                studentCourseMessages = studentCourseMessageService.findByCno(name, (List<StudentCourseMessage>) allStudentList.getRows());
            } else if (searchOption.equals("11")) {//以课程名查询
                studentCourseMessages = studentCourseMessageService.findByCname(name, (List<StudentCourseMessage>) allStudentList.getRows());
            } else  {//以教师名查询
                studentCourseMessages = studentCourseMessageService.findByTeacher(name, (List<StudentCourseMessage>) allStudentList.getRows());
            }
        }
        int studentCourseMessagesSize = studentCourseMessages.size();
        studentCourseMessages = studentCourseMessages.stream().skip((pageNum - 1) * size).limit(size).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(studentCourseMessages, studentCourseMessagesSize);

        return pageUtils;
    }

    /**
     * 用于成绩查询里的总览表
     * @param page
     * @param size
     * @param pageNum
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/student/displayList")
    public PageUtils studentGetByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                               HttpServletRequest httpServletRequest) throws Exception{
        HttpSession session = httpServletRequest.getSession();
        if (!session.getAttribute("choose").equals("1") || session.getAttribute("userid") == null){
            throw new RException(REnum.LOGIN_ERR);
        }
        //序号，课号，课名，课时，学分，分数，授课教师
        Student studentMessage = (Student) session.getAttribute("user");

        List<SelectCourse> studentSelectCourse = selectCourseService.findBySno(studentMessage.getSno());
        List<StudentCourseMessage> studentCourseMessages = new ArrayList<>();

        for (int i = 0;i < studentSelectCourse.size();i++){
            StudentCourseMessage studentCourseMessage = new StudentCourseMessage();
            studentCourseMessage.setId(i+1);
            if (teacherCourseService.findByCno(studentSelectCourse.get(i).getCno()).size() > 0){ //通过学生的选课表Cno查询课号教师授课表Cno，若找到又找教师表查到授课教师工号
                studentCourseMessage.setTeacher(teacherService.findByTno(teacherCourseService.findByCno(studentSelectCourse.get(i).getCno()).get(0).getTno()).getTname());//设置课程教师
            }
            studentCourseMessage.setScore(studentSelectCourse.get(i).getScore());//设置学生成绩
            if (courseService.findByCno(studentSelectCourse.get(i).getCno()) != null){
                studentCourseMessage.setCname(courseService.findByCno(studentSelectCourse.get(i).getCno()).getCname());//设置课程名
                studentCourseMessage.setCredit(courseService.findByCno(studentSelectCourse.get(i).getCno()).getCredit());//设置学分
                studentCourseMessage.setHour(courseService.findByCno(studentSelectCourse.get(i).getCno()).getHour());//设置学时
                studentCourseMessage.setCno(studentSelectCourse.get(i).getCno());
            }
            studentCourseMessages.add(studentCourseMessage);
        }

        //设置分页
        int studentCourseMessagesSize = studentCourseMessages.size();
        studentCourseMessages = studentCourseMessages.stream().skip((pageNum-1)*size).limit(size).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(studentCourseMessages,studentCourseMessagesSize);

        return pageUtils;
    }

}


